function addNewSupport(data){
	$.ajax({
		type:'POST',
		url:'api.cfm?api=add_added_staff',
		dataType: 'json',
		data:data,
		async: true,
		complete: function(json) {
			getSupport();
		}
	});
}



function deleteSupport(data){

	$.ajax({
		type:'POST',
		url:'api.cfm?api=delete_added_staff',
		dataType: 'json',
		data:data,
		async: true,
		complete: function(json) {
			getSupport();
		}
	});	
}



function getSupport(){
	var jdata = "";
	$.ajax({
		type:'GET',
		url:'api.cfm?api=get_added_staff',
		dataType: 'json',
		async: true,
		success: function(json) {
			jdata = json
		},
		complete: function(){
			$("#support_list").children().remove();
			$.each(jdata.DATA, function(n, i){
				$("#support_list").append("<option value='" + i[0] + "'>" + i[2] + ", " + i[1] + 
					" ( " + i[5] + " - " + i[3] + " )</option>");				
		    });	
		}
	});
}


function bindButtons(){

	$("#support_list").bind("change", function(){
		$("#delete_staff").remove();
		$("#delete_area").after("<input type='button' id='delete_staff' value='Delete Selected'");
		var form_data = {
			"AddedID" : $(this).val()[0],
			"Action" : "delete"
		};		
		$("#delete_staff").bind("click", function(){
			deleteSupport(form_data);
			getSupport();
			$("#delete_staff").val("");
			$("#delete_staff").remove();
		});
	});
	
	
	
	$("#save_btn").bind("click", function(){
		if ( $("#first_name").val() != "" && $("#last_name").val() != "" && $("#type").val() != ""){
			 var form_data = {
				"FirstName" : $("#first_name").val(),
				"LastName" : $("#last_name").val(),
				"Clinic" : $("#clinic option:selected").val(),
				"Phone" : '',
				"SupportType" : $("#type").val(),
				"Action" : "add"
			};
			addNewSupport(form_data);
			clearText();
		}		
	});
}


function clearText(){
	$("#first_name").val("");
	$("#last_name").val("");
	$("#clinic").val("");
	$("#phone").val("");
	$("#type").val("");
}


var validatePhone = function(phone){
	var re = '^[0-9]{10}$';
	if( !phone.match(new RegExp(re)) ){
		return false;	
	}
	return true;
};


$(function() {
	getSupport();
	bindButtons();
});